﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace H.Utils
{
    /// <summary>
    /// 此工具类用于处理ID序列
    /// </summary>
    public static class IDUtil
    {
        /// <summary>
        /// 将一个ID序列切分为多个ID组成的数组
        /// </summary>
        /// <param name="srcStr">传入要进行切割的ID序列字符串</param>
        /// <returns>返回切割后的ID数组</returns>
        public static string[] SplitIds(string srcStr)
        {
            if (string.IsNullOrEmpty(srcStr))
            {
                return new string[0];
            }
            else
            {
                return srcStr.Split(',');
            }
        }

        /// <summary>
        /// 将一个ID序列切分为多个整形ID的数组
        /// </summary>
        /// <param name="srcStr">传入要进行切割的ID序列字符串,</param>
        /// <returns>返回切割后的ID数组</returns>
        public static int[] SplitInts(string srcStr)
        {
            if (string.IsNullOrEmpty(srcStr))
            {
                return new int[0];
            }
            else
            {
                string[] vals = srcStr.Split(',');
                int[] res = new int[vals.Length];
                for (int i = 0; i < vals.Length; i++)
                {
                    int.TryParse(vals[i], out res[i]);
                }
                return res;
            }
        }

        /// <summary>
        /// 调换两个变量的值
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="v1">值1</param>
        /// <param name="v2">值2</param>
        public static void Swap<T>(ref T v1, ref T v2)
        {
            T v = v1;
            v1 = v2;
            v2 = v;
        }

        /// <summary>
        /// 往一个ID序列中加入一个新的ID
        /// </summary>
        /// <param name="srcStr">原始ID序列</param>
        /// <param name="newID">新增的ID</param>
        /// <returns>返回生成的新ID序列</returns>
        public static string AddID(string srcStr, string newID)
        {
            string[] ids = SplitIds(srcStr);

            if (Array.IndexOf(ids, newID) != -1)
            {
                return srcStr;
            }
            else
            {
                return ids.Length > 0 ? srcStr + "," + newID : newID;
            }
        }

        /// <summary>
        /// 在一个ID序列中删除指定的ID
        /// </summary>
        /// <param name="srcStr">原始ID序列</param>
        /// <param name="id">要删除的ID</param>
        /// <returns>返回生成的新ID序列</returns>
        public static string RemoveID(string srcStr, string id)
        {
            string[] ids = SplitIds(srcStr);
            int idx = Array.IndexOf(ids, id);
            if (idx != -1)
            {
                return string.Join(",", from i in ids where i != id select i);
            }
            else
            {
                return srcStr;
            }
        }

        /// <summary>
        /// 将某一个ID排到第一个
        /// </summary>
        /// <param name="srcStr">原始ID序列</param>
        /// <param name="id">欲操作的ID</param>
        /// <returns>返回生成的新ID序列</returns>
        public static string TopID(string srcStr, string id)
        {
            string[] ids = SplitIds(srcStr);
            int idx = Array.IndexOf(ids, id);
            if (idx != -1)
            {
                return id + "," + string.Join(",", from i in ids where i != id select i);
            }
            else
            {
                return srcStr;
            }
        }

        /// <summary>
        /// 将一个ID向前挪动一位
        /// </summary>
        /// <param name="srcStr">原始ID序列</param>
        /// <param name="id">欲操作的ID</param>
        /// <returns>返回生成的新ID序列</returns>
        public static string UpID(string srcStr, string id)
        {
            string[] ids = SplitIds(srcStr);
            int idx = Array.IndexOf(ids, id);
            if (idx > 0)
            {
                Swap(ref ids[idx], ref ids[idx - 1]);
                return string.Join(",", ids);
            }
            else
            {
                return srcStr;
            }
        }

        /// <summary>
        /// 将一个ID向后挪动一位
        /// </summary>
        /// <param name="srcStr">原始ID序列</param>
        /// <param name="id">欲操作的ID</param>
        /// <returns>返回生成的新ID序列</returns>
        public static string DownID(string srcStr, string id)
        {
            string[] ids = SplitIds(srcStr);
            int idx = Array.IndexOf(ids, id);
            if (idx < ids.Length - 1)
            {
                Swap(ref ids[idx], ref ids[idx + 1]);
                return string.Join(",", ids);
            }
            else
            {
                return srcStr;
            }
        }

        /// <summary>
        /// 将一个ID挪到最底部
        /// </summary>
        /// <param name="srcStr">原始ID序列</param>
        /// <param name="id">欲操作的ID</param>
        /// <returns>返回生成的新ID序列</returns>
        public static string BottomID(string srcStr, string id)
        {
            string[] ids = SplitIds(srcStr);
            int idx = Array.IndexOf(ids, id);
            if (idx != -1)
            {
                return string.Join(",", from i in ids where i != id select i) + "," + id;
            }
            else
            {
                return srcStr;
            }
        }

    }
}
